﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace Selenium.PageToolkit
{
    public class PageElementAttribute : Attribute, IElementSpecFactory
    {
        private readonly string _css;
        private readonly string _xpath;
        private readonly string _id;
        private readonly string _class;
        private readonly bool _cache;

        public PageElementAttribute(string css = null, string xpath=null, string id=null, string @class = null, bool cache = false)
        {
            _css = css;
            _xpath = xpath;
            _id = id;
            _class = @class;
            _cache = cache;
        }

        public By Spec()
        {
            if (_css != null)
                return By.CssSelector(_css);
            if (_xpath != null)
                return By.XPath(_xpath);
            if (_class != null)
                return By.ClassName(_class);
            if (_id != null)
                return By.Id(_id);
            throw new Exception("Unable to generate selector");
        }

        public bool Cache
        {
            get { return _cache; }
        }
    }
}
